package com.wuye.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wuye.entity.Users;
import com.wuye.service.UsersService;

@Controller
@RequestMapping("/user")
public class UserAction {
	@Resource
	private UsersService usersService;
	//增加
	@RequestMapping("/save")
	public String save(Users users,HttpServletRequest request){
		usersService.save(users);
		request.setAttribute("msg", "已添加"+users.getName());
		return "../yz.jsp";
	}
	//删除
	@RequestMapping("/del")
	public String del(String str,HttpServletRequest request){
		try {
			usersService.del(str);
			request.setAttribute("msg", "已删除");
		} catch (Exception e) {
			request.setAttribute("msg", "删除业主中有已录入的其他信息,删除失败");
			e.printStackTrace();
		}
		return "../yz.jsp";
	}
	//更新
	@RequestMapping("/update")
		public String update(Users users,HttpServletRequest request){
			usersService.update(users);
			request.setAttribute("msg", "已更新"+users.getName());
			return "../yz.jsp";
		}
	//列表
	@RequestMapping("/allList")
	public @ResponseBody Map getAllList(String page,String rows,HttpServletRequest request){
		int intPage = Integer.parseInt((page == null || page =="0") ? "1":page); 
        int number = Integer.parseInt((rows == null || rows =="0") ? "10":rows); 
        int start = (intPage-1)*number; 
        return usersService.getList(start,number);
	}
	//查询
	@RequestMapping("/searchList")
	public @ResponseBody Map getSearchList(Users users,String page,String rows,HttpServletRequest request) throws Exception{
		int intPage = Integer.parseInt((page == null || page =="0") ? "1":page); 
        int number = Integer.parseInt((rows == null || rows =="0") ? "10":rows); 
        int start = (intPage-1)*number; 
        return usersService.getSearchList(users,start,number);
	}
	//修改密码
	@RequestMapping("/rePwd")
	public String rePwd(int id,String newPwd,HttpServletRequest request){
		usersService.rePwd(id,newPwd);
		request.setAttribute("msg", "已修改密码");
		return "../index.jsp";
	}
	//登录
	@RequestMapping("/login")
	public String login(Users users,HttpServletRequest request){
		Users newUsers= usersService.login(users);
		if(newUsers==null){
			request.setAttribute("msg", "用户名或密码错误");
			return "../login.jsp";
		}else{
			System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+newUsers.getName());
			request.getSession().setAttribute("sessionid", newUsers);
			return "../index.jsp";
		}
	}
	public UsersService getUsersService() {
		return usersService;
	}
	public void setUsersService(UsersService usersService) {
		this.usersService = usersService;
	}
}
